package com.app.newiptv.presenter;

import android.content.Context;

import com.app.newiptv.bean.Content;
import com.app.newiptv.bean.allchannels.Program;
import com.app.newiptv.callback.IRequestCallback;
import com.app.newiptv.service.AHservice;
import com.app.newiptv.utils.Paths;
import com.app.newiptv.utils.SystemTimeDate;

import java.util.List;


/**
 * Created by wendal on 2017/9/17.
 */

public class PlayerPresneter {
    private boolean isDisplayed;
    private Callback callback;
    private List<Content> listContent;
    private List<Content> vodContent;
    private AHservice service;
    private Context mContext;
    private Program program;
    private int index = -1;

    public PlayerPresneter(Context mContext) {
        this.mContext = mContext;
    }

    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    public interface Callback {
        void onError(int code, String msg);

        void onPlayerUrlCompleted(PlayerPresneter presenter, int count);
    }

    public interface ItemViewHolder {
        void setChannelName(String name);

        void setCurrentPlay(String text);

        void setCurrentPlayTime(String text);

        void setLaterPlay(String text);

        void setLaterPlaytime(String text);
    }

    public void setDisplayed(boolean displayed) {
        isDisplayed = displayed;
    }

    public void loadVodItem(int id) {
        service = new AHservice();
        service.loadVodItemData(Paths.HOSTPATH + Paths.PROGRAM_LIVE + "/"+id, new ItemLoadCallback());
    }


    public void onBindMovieView(ItemViewHolder holder, int position) {
        if (!isDisplayed) {
            return;
        }
        holder.setChannelName(program.getName());
        List<Program.Epg> epgList = program.getEpg();
        if (epgList != null) {
            for (int i = 0; i < epgList.size(); i++) {
                Program.Epg epg = epgList.get(i);
                String startTime = epg.getStarttime();
                String endTime = epg.getEndtime();
                int startV = SystemTimeDate.compare_date(Long.parseLong(startTime) * 1000, System.currentTimeMillis());
                int endV = SystemTimeDate.compare_date(Long.parseLong(endTime) * 1000, System.currentTimeMillis());
                if (startV == -1 && endV == 1) {
                    index = i;
                }
            }
            if (index >= 0 && index < epgList.size()) {
                holder.setCurrentPlay("当前播放：" + epgList.get(index).getTitle());
                String startTime = SystemTimeDate.stampToHHmmDate(String.valueOf(Long.parseLong(epgList.get(index).getStarttime()) * 1000));
                String endTime = SystemTimeDate.stampToHHmmDate(String.valueOf(Long.parseLong(epgList.get(index).getEndtime()) * 1000));
                holder.setCurrentPlayTime(startTime + " - " + endTime);
                if (index + 1 < epgList.size()) {
                    holder.setLaterPlay("稍后播放：" + epgList.get(index + 1).getTitle());
                    String startLaterTime = SystemTimeDate.stampToHHmmDate(String.valueOf(Long.parseLong(epgList.get(index + 1).getStarttime()) * 1000));
                    String endLaterTime = SystemTimeDate.stampToHHmmDate(String.valueOf(Long.parseLong(epgList.get(index + 1).getEndtime()) * 1000));
                    holder.setLaterPlaytime(startLaterTime + " - " + endLaterTime);
                }
            }
        }

    }

    class ItemLoadCallback implements IRequestCallback {

        @Override
        public void onLoadComplete(String contentID, int pageNumber, int count, Object dto) {
            if (dto != null && dto instanceof Program) {
                program = (Program) dto;
                if (callback != null) {
                    callback.onPlayerUrlCompleted(PlayerPresneter.this, 0);
                }
            }
        }

        @Override
        public void onLoading(String contentID) {

        }

        @Override
        public void onFailed(String code, String msg) {

        }
    }

}
